package com.app.qbake.module.square;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wind.base.mvp.view.TabLayoutFragment;
import com.wind.base.utils.ViewInflater;
import com.wind.data.square.bean.Seller;
import com.app.qbake.R;
import com.app.qbake.module.square.event.SellerInfoEvent;
import com.app.qbake.module.square.layout.FollowLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SellerHomeTabFragment extends TabLayoutFragment implements FollowLayout.OnFollowListener {
    public static final String TYPE_GOODS = "shop";
    public static final String TYPE_VIDEO = "video";

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_seller_tab;
    }

    private String toUid;

    @BindView(R.id.iv_seller_avatar)
    ImageView iv_seller_avatar;

    @BindView(R.id.tv_seller_name)
    TextView tv_seller_name;
    @BindView(R.id.tv_seller_introduce)
    TextView tv_seller_introduce;
    @BindView(R.id.tv_seller_goods_num)
    TextView tv_seller_goods_num;
    @BindView(R.id.tv_seller_fans_num)
    TextView tv_seller_fans_num;

    @BindView(R.id.tv_follow)
    TextView tv_follow;

    FollowLayout mFollowLayout;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        toUid = getArguments().getString(SellerGoodsListFragment.ARGS_KEY_UID);
        EventBus.getDefault().register(this);
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
        mFollowLayout=new FollowLayout(getActivity(),this);
        tv_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean followed=mSeller.getIs_follow()==1;
                if (followed){
                    mFollowLayout.unfollow(toUid);
                }else {
                    mFollowLayout.follow(toUid);
                }

            }
        });
    }

    @Override
    public List<String> getTitles() {
        List<String> titles = new ArrayList<>();
        titles.add("宝贝");
        titles.add("视频");
        return titles;
    }

    @Override
    protected List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(SellerGoodsListFragment.getInstance(toUid));
        fragments.add(SellerVideoListFragment.getInstance(toUid));
        return fragments;
    }

    public static SellerHomeTabFragment getInstance(String toUid) {
        SellerHomeTabFragment f = new SellerHomeTabFragment();
        Bundle args = new Bundle();
        args.putString(SellerGoodsListFragment.ARGS_KEY_UID, toUid);
        f.setArguments(args);
        return f;
    }

    private Seller mSeller;
    @Subscribe
    public void onSellerInfoEvent(SellerInfoEvent event) {
        mSeller = event.getSeller();
        ViewInflater.inflateImage(iv_seller_avatar, mSeller.getAvatar());
        ViewInflater.inflateText(tv_seller_name, mSeller.getUserName());
        ViewInflater.inflateText(tv_seller_introduce, mSeller.getIntroduce());
        ViewInflater.inflateText(tv_seller_goods_num, "商品" + mSeller.getShop_number() + "");
        ViewInflater.inflateText(tv_seller_fans_num, "粉丝" + mSeller.getFollow_number() + "");

        boolean followed=mSeller.getIs_follow()==1;
        if (followed){
            tv_follow.setText("取消关注");
        }else {
            tv_follow.setText("关注");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onFollowReturn(String type) {
        if (FollowLayout.FOLLOW.equals(type)){
            tv_follow.setText("取消关注");
            mSeller.setIs_follow(1);
        }else {
            tv_follow.setText("关注");
            mSeller.setIs_follow(0);
        }
    }
}
